package com.bolt.auth.security.admin.biz.sys.repository;


import com.bolt.auth.admin.api.sys.entity.RoleEntity;
import com.bolt.auth.admin.api.sys.entity.UserEntity;
import com.bolt.support.spring.jpa.repository.BasicJpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public interface RoleRepository extends BasicJpaRepository<RoleEntity, String> {

	@Query("from RoleEntity order by code asc")
	List<RoleEntity> findAllCached();

    RoleEntity findByCode(String code);


    @Query("select distinct r from UserR2Role u2r,RoleEntity r "
			+ "where u2r.role=r "
			+ "and u2r.user=:user and r.enabled=true order by r.code asc")
	List<RoleEntity> findRolesByUser(@Param("user") UserEntity user);


    @Query("select distinct r from UserR2Role u2r,RoleEntity r "
            + "where u2r.role=r "
            + "and u2r.userId=:userId and r.enabled=true order by r.code asc")
    List<RoleEntity> findRolesByUserId(@Param("userId") String userId);



    @Query("select id,name from RoleEntity where enabled=true order by code asc")
	List<Object[]> findAllRoleItem();
	
	
	
	

}